package animation;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import main.GameCanvas;
import utilities.FrameImage;
import utilities.GameResource;

public class Smoke extends Effect
{
	FrameImage frmImg;
	Image imgStun;
	int vx, vy;
	int curFrame;
	int delay;
	byte type;
	byte speed;
		
	public static final byte SMOKE = 0;
	public static final byte ROCK = 2;
	public static final byte ROCK_2 = 3;
	public static final byte HEART = 6;	
	public static final byte STUN = 7;
	public static final byte STAR = 8;
	public static final byte EFFECTS = 9;
	public static final byte EFFECTS1 = 10;
	public static final byte EFFECTS2 = 11;
	public static final byte EFFECTS3 = 12;
	
	
	public void startEffect(int x, int y, int vx, int vy, byte Type){
		super.startEffect(x, y);
		
		this.vx = vx;
		this.vy = vy;
		this.type = Type;
		curFrame = 0;
		delay = 0;
		speed = 0;
		
		switch (Type) {
		case ROCK_2:
			frmImg = new FrameImage(GameResource.instance.rock2Img, 10, 10);
			break;
		case HEART:
			frmImg = new FrameImage(GameResource.instance.imgHeart, 19, 13);
			break;
		case SMOKE:
			frmImg = new FrameImage(GameResource.instance.smoke, 30, 30);
			break;
		case STAR:
		case STUN:
			imgStun = GameResource.instance.imgStun;
			break;
		case EFFECTS:
			frmImg = new FrameImage(GameResource.instance.imgEffect[0], 7, 7);
			break;
		case EFFECTS1:
			frmImg = new FrameImage(GameResource.instance.imgEffect[1], 7, 7);
			break;
		case EFFECTS2:
			frmImg = new FrameImage(GameResource.instance.imgEffect[2], 7, 7);
			break;
		case EFFECTS3:
			frmImg = new FrameImage(GameResource.instance.imgEffect[3], 7, 7);
			break;
		}
	}
	
	public void startEffect(int x, int y, int vx, int vy, byte speed, byte Type) {
		// TODO Auto-generated method stub
		super.startEffect(x, y);
		this.vx = vx;
		this.vy = vy;
		this.type = Type;
		this.speed = speed;
		curFrame = 0;
		delay = 0;
		
		switch (Type) {
		case ROCK_2:
			frmImg = new FrameImage(GameResource.instance.rock2Img, 10, 10);
			break;
		case HEART:
			frmImg = new FrameImage(GameResource.instance.imgHeart, 19, 13);
			break;
		case SMOKE:
			frmImg = new FrameImage(GameResource.instance.smoke, 30, 30);
			break;
		case STAR:
		case STUN:
			imgStun = GameResource.instance.imgStun;
			break;
		case EFFECTS:
			frmImg = new FrameImage(GameResource.instance.imgEffect[0], 7, 7);
			break;
		case EFFECTS1:
			frmImg = new FrameImage(GameResource.instance.imgEffect[1], 7, 7);
			break;
		case EFFECTS2:
			frmImg = new FrameImage(GameResource.instance.imgEffect[2], 7, 7);
			break;
		case EFFECTS3:
			frmImg = new FrameImage(GameResource.instance.imgEffect[3], 7, 7);
			break;
		}
	}
	
	public void update()
	{
		switch (type)
		{
		case SMOKE:
			if (m_iGameTick > 5 || m_posCenterEffect.y < 0)
			{
				GameCanvas.m_arrEffect.removeElement(this);
				return;
			}
						
			updateFrame(1);
			
			if (curFrame > 4)
				curFrame = 4;
			m_posCenterEffect.x += vx;
			m_posCenterEffect.y -= vy;
			vy += 1;
			break;
		case ROCK:
		case ROCK_2:
			if (type == ROCK_2)
			{
				updateFrame(1);
				curFrame += 4;
				if (curFrame > 8)
					curFrame = 4;
			}
			if (m_iGameTick > 20 || m_posCenterEffect.y > GameCanvas.h)
			{
				GameCanvas.m_arrEffect.removeElement(this);
				return;
			}
			m_posCenterEffect.x += vx;
			m_posCenterEffect.y += vy;
			vy += 1;
			break;
		case HEART:
			updateFrame(4);
			if (vy > 0){
				vx = 1;				
				if (curFrame > 3)
					curFrame = 0;
			}else
				curFrame = 0;

			if (m_posCenterEffect.y > GameCanvas.h - 20)	{
				GameCanvas.m_arrEffect.removeElement(this);
				return;
			}
			m_posCenterEffect.x += vx;
			m_posCenterEffect.y += vy;
			if(vy < 2)
				vy += 1;
			break;
		case STAR:
			if(vx > 0)
				m_posCenterEffect.x += speed;
			else if(vx < 0)
				m_posCenterEffect.x -= speed;
			if(vy > 0)
				m_posCenterEffect.y += speed;
			else if(vy < 0)
				m_posCenterEffect.y -= speed;
			if(curFrame>1)
				m_posCenterEffect.y -= 2;
		case STUN:
			updateFrame(speed);
			if(m_iGameTick > 100){
				GameCanvas.m_arrEffect.removeElement(this);
				return;
			}
			break;
		case EFFECTS:
		case EFFECTS1:
		case EFFECTS2:
		case EFFECTS3:
			updateFrame(4);
			if (m_iGameTick > speed || m_posCenterEffect.y > GameCanvas.h)
			{
				GameCanvas.m_arrEffect.removeElement(this);
				return;
			}
			m_posCenterEffect.x += vx;
			m_posCenterEffect.y += vy;
			vy += 1;
			break;
		}
		
		m_iGameTick++;
	}

	private void updateFrame(int maxFrame)
	{
		delay++;
		if (delay > maxFrame)
		{
			curFrame++;
			delay = 0;
		}
	}

	public void paint(Graphics g)
	{
		switch (type)
		{
		case SMOKE:
			frmImg.drawFrame(curFrame, m_posCenterEffect.x, m_posCenterEffect.y, 0, Graphics.HCENTER | Graphics.VCENTER, g);
			break;
		case ROCK:
			g.drawImage(GameResource.instance.rockImg, m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER | Graphics.VCENTER);
			break;
		case ROCK_2:
			frmImg.drawFrame(curFrame, m_posCenterEffect.x, m_posCenterEffect.y, 0, Graphics.HCENTER | Graphics.VCENTER, g);
			break;
		case HEART:
			frmImg.drawFrame(curFrame, m_posCenterEffect.x, m_posCenterEffect.y, 0, Graphics.HCENTER | Graphics.VCENTER, g);
			break;
		case STUN:
			switch (curFrame) {
			case 0:
				g.drawRegion(imgStun, 0, 56, 16, 16, Sprite.TRANS_NONE,  m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER|Graphics.VCENTER);
				break;
			case 1:
				g.drawRegion(imgStun, 0, 72, 33, 32, Sprite.TRANS_NONE,  m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER|Graphics.VCENTER);
				break;
			case 2:
				g.drawRegion(imgStun, 0, 104, 16, 16, Sprite.TRANS_NONE,  m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER|Graphics.VCENTER);
				break;
			}
			break;
			
		case STAR:
			switch (curFrame) {
			case 3:
				g.drawRegion(imgStun, 0, 0, 22, 21, Sprite.TRANS_NONE,  m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER|Graphics.VCENTER);
				break;
			case 2:
				g.drawRegion(imgStun, 0, 21, 16, 16, Sprite.TRANS_NONE,  m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER|Graphics.VCENTER);
				break;
			case 1:
				g.drawRegion(imgStun, 0, 37, 10, 11, Sprite.TRANS_NONE,  m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER|Graphics.VCENTER);
				break;
			case 0:
				g.drawRegion(imgStun, 0, 48, 8, 8, Sprite.TRANS_NONE,  m_posCenterEffect.x, m_posCenterEffect.y, Graphics.HCENTER|Graphics.VCENTER);
				break;
			}
			break;
			
		case EFFECTS:
			frmImg.drawFrame(curFrame, m_posCenterEffect.x, m_posCenterEffect.y, 0, Graphics.HCENTER | Graphics.VCENTER, g);
			break;
		case EFFECTS1:
			frmImg.drawFrame(curFrame, m_posCenterEffect.x, m_posCenterEffect.y, 0, Graphics.HCENTER | Graphics.VCENTER, g);
			break;
		case EFFECTS2:
			frmImg.drawFrame(curFrame, m_posCenterEffect.x, m_posCenterEffect.y, 0, Graphics.HCENTER | Graphics.VCENTER, g);
			break;
		case EFFECTS3:
			frmImg.drawFrame(curFrame, m_posCenterEffect.x, m_posCenterEffect.y, 0, Graphics.HCENTER | Graphics.VCENTER, g);
			break;
		}
	}
}
